package com.xzt.meeting.dao;

import com.xzt.meeting.entity.Dept;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/7 14:16
 * @Version 1.0
 * @Introduce
 */
public class DeptDao extends BaseDao{

    public List<Map<String, Object>> findAll() {
        String sql="SELECT\n" +
                "\td.*,\n" +
                "\tcount( u.dept_id ) deptCount \n" +
                "FROM\n" +
                "\tdept d\n" +
                "\tLEFT JOIN USER u ON d.id = u.dept_id \n" +
                "GROUP BY\n" +
                "\td.id";

        return jdbcTemplate.queryForList(sql);
    }

    public List<Dept> findAllList() {
        return jdbcTemplate.query("select * from dept",new BeanPropertyRowMapper<>(Dept.class));
    }

    public Dept findById(Long deptId) {
        try{
            return jdbcTemplate.queryForObject("select * from dept where id=?",
                    new BeanPropertyRowMapper<>(Dept.class),deptId);
        }catch (EmptyResultDataAccessException throwables){
            throwables.printStackTrace();
        }
        return null;
    }
}
